#include "widget.h"
#include "ui_widget.h"
#include "my_label.h"
#include <QLabel>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QTimer>
#include <QLCDNumber>
#include <QTimerEvent>
#include <QMoveEvent>
#include <QResizeEvent>
#include <QAction>
#include <QPlainTextEdit>
#include <QMenuBar>
#include <QFileDialog>
#include <QMainWindow>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    /*
    ui->setupUi(this);
    my_label* label = new my_label(this);
    label->setText("this is a label");
    label->setFrameShape(QLabel::Box);
    this->setMouseTracking(true); //告诉qt就是要追踪鼠标踪迹

    QLCDNumber* number = new QLCDNumber(this);
    number->move(500, 500);
    number->display(10);
    QTimer* timer = new QTimer(this);
    timer->start(1000);
    connect(timer, &QTimer::timeout, this, [this, number, timer](){
        int value = number->intValue();
        if(value <= 0)
            {
            timer->stop();
            return;
        }
        else
            {
            number->display(value - 1);
        }
    });

    //带起定时器事件，并返回表示timer的标识.
    ui->lcdNumber->display(10);
    timerId = this->startTimer(1000);
    */

    /////////////QFile/////////////

    //open, read/readLine/readAll, write, close
    QMenuBar* menubar = new QMenuBar(this);

    QMenu* menu = new QMenu();
    menubar->addMenu(menu);

    QAction* action_1 = new QAction("保存");
    QAction* action_2 = new QAction("打开");
    menu->addAction(action_1);
    menu->addAction(action_2);

    QPlainTextEdit* edit = new QPlainTextEdit();
    this->setCentralWidget(edit);

    connect(action_1, &QAction::triggered, this, [this](){
        QString filename = QFileDialog::getSaveFileName();
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mouseMoveEvent(QMouseEvent* event)
{
    qDebug() << event->x() << ", " << event->y();
}

void Widget::wheelEvent(QWheelEvent* event)
{
    (void)event;
    //total += event->delta();
}

void Widget::keyPressEvent(QKeyEvent* event)
{
    int key = event->key();
    if(key == Qt::Key_A)
    {
        qDebug() << "A被按下了";
    }
    else if(key == Qt::Key_A && event->modifiers() == Qt::ControlModifier)
    {
        qDebug() << "Ctrl + A 被按下了";
    }
}

void Widget::timerEvent(QTimerEvent* event)
{
    if(event->timerId() == this->timerId)
    {
        //是我们想要的定时器触发的
        int value = ui->lcdNumber->intValue();
        if(value > 0)
        {
            ui->lcdNumber->display(value - 1);
        }
        else
        {
            this->killTimer(timerId); //终止定时器
            return;
        }
    }
    else
    {
        return;
    }
}

void Widget::moveEvent(QMoveEvent* event)
{
    qDebug() << event->pos();
}

void Widget::resizeEvent(QResizeEvent* event)
{
    qDebug() << event->size();
}
